(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

open XNum
open UTypes
open Substitute
  
type glyph_extra_info =
  | GXI_Normal
  | GXI_LigKern of int
  | GXI_List of int
  | GXI_Extendable of int * int * int * int

type extra_kern_info = {
  ki_after_space : num;
  ki_before_space : num;
  ki_after_margin : num;
  ki_before_margin : num;
  ki_after_foreign : num;
  ki_before_foreign : num
}

type glyph_metric = {
  gm_width : num;
  gm_height : num;
  gm_depth : num;
  gm_italic : num;
  gm_extra : glyph_extra_info;
  gm_extra_kern : extra_kern_info
}

let zero_kern_info = {
  ki_after_space = num_zero;
  ki_before_space = num_zero;
  ki_after_margin = num_zero;
  ki_before_margin = num_zero;
  ki_after_foreign = num_zero;
  ki_before_foreign = num_zero;
}
  
let empty_glyph_metric = {
  gm_width = num_zero;
  gm_height = num_zero;
  gm_depth = num_zero;
  gm_italic = num_zero;
  gm_extra = GXI_Normal;
  gm_extra_kern = zero_kern_info;
}
  
(* The the after values from <left> and the before ones from <right>. *)
let merge_kern_infos left right = {
  ki_after_space = left.ki_after_space;
  ki_before_space = right.ki_before_space;
  ki_after_margin = left.ki_after_margin;
  ki_before_margin = right.ki_before_margin;
  ki_after_foreign = left.ki_after_foreign;
  ki_before_foreign = right.ki_before_foreign;
}
  
type lig_kern =
  | NoLigKern
  | Ligature of int * int * bool * bool (* glyph skip keep-first? keep-second? *)
  | Kern of num

type ('box, 'cmd) char_item =
    [
    | `Char of uc_char
    | `Kern of num * num
    | `Box of 'box
    | `Command of 'cmd
    | `Break of (num * bool *
                   ('box, 'cmd) char_item list *
                   ('box, 'cmd) char_item list *
                   ('box, 'cmd) char_item list)
    ]
